package com.tbs.easyhomemoneyserver.config;

import com.tbs.easyhomemoneyserver.constants.CommonConstants;
import com.tbs.easyhomemoneyserver.model.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;


/**
 * 拦截器异常返回接口，用于拦截器中异常处理
 */
@RestController
@Slf4j
public class ExceptionController implements ErrorController {

    @RequestMapping(CommonConstants.ERROR_PATH)
    public ResultVO handleError(ServletRequest request)  {
        Object obj = request.getAttribute(CommonConstants.ERROR_ATTR_NAME);
        if (obj != null) {
            log.error("error catch",(Throwable) obj);
            return ResultVO.error((Throwable) obj);
        } else {
            log.error("UNKNOWN ERROR");
            return ResultVO.error(new RuntimeException("UNKNOWN ERROR"));
        }

    }
}
